
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },
  bindGetUserInfo: function (e) {
    if (!e.detail.userInfo){
      return;
    }
    wx.setStorageSync('userInfo', e.detail.userInfo)
    // wx.navigateBack({ changed: true });//返回上一页 
    var self = this;
    getOpenId(self);
  },
})

//获取OpenId
var getOpenId = function (self) {
  wx.login({
    success: function (res) {
      console.log(res)
      wx.request({
        url: app.globalData.ServiceUrl + 'qiniufile/GetOpenid', //接口地址
        method: 'POST',
        data: {
          ValidateData: app.globalData.ValidateData,
          paras: {
            js_code: res.code
          }
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          console.log(res.data)
          if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
            app.globalData.OPEN_ID = res.data.UserArea.openid;//获取到的openid
            app.globalData.SESSION_KEY = res.data.UserArea.session_key;//获取到session_key
            // wx.navigateBack({ changed: true });//返回上一页
            wx.setStorageSync('OPEN_ID', res.data.UserArea.openid)
            wx.navigateBack({ changed: true });//返回上一页
            // register(self)
          } else {
            wx.showToast({
              title: '获取数据失败！',
              image: '../../images/img_jinggao.png',
              duration: 1000,
            })
          }
        }
      })
    }
  })
}

var register = function (self) {
  let userInfo = wx.getStorageSync('userInfo')
  var location = userInfo.country + "-" + userInfo.province + "-" + userInfo.city
  console.log(userInfo)
  wx.request({
    url: app.globalData.ServiceUrl + 'user/register', //接口地址
    method: 'POST',
    data: {
      ValidateData: app.globalData.ValidateData,
      paras: {
        userName: userInfo.nickname,
        userGuid: app.globalData.OPEN_ID,
        photoUrl: userInfo.avatarUrl,
        sex: userInfo.gender,
        adress: location,
        telephoneNum: ''
      }
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success: function (res) {
      console.log(res.data)
      wx.showModal({
        content: '绑定成功,您可以修改该用户的信息了',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
    }
  })
}